package com.aqa.concurrent;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

public class ExecutorServiceTest {

    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
    private static final int COUNT_BITS = Integer.SIZE - 3;
    private static final int CAPACITY = (1 << COUNT_BITS) - 1;

    // runState is stored in the high-order bits
    private static final int RUNNING = -1 << COUNT_BITS;
    private static final int SHUTDOWN = 0 << COUNT_BITS;
    private static final int STOP = 1 << COUNT_BITS;
    private static final int TIDYING = 2 << COUNT_BITS;
    private static final int TERMINATED = 3 << COUNT_BITS;

    // Packing and unpacking ctl
    private static int runStateOf(int c) {
        return c & ~CAPACITY;
    }

    private static int workerCountOf(int c) {
        return c & CAPACITY;
    }

    private static int ctlOf(int rs, int wc) {
        return rs | wc;
    }

    @Test
    public void threadPoolTest() {
        System.out.println("--------------------------");
    }

    @Test
    public void threadPoolExecutorTest() throws InterruptedException {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 10, 10L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
        poolExecutor.allowCoreThreadTimeOut(true);
        poolExecutor.execute(() -> {
            try {
                System.out.println("------------ print something ----------------");
                Thread.sleep(1000);
                System.out.println("------------ print something done ----------------");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println("------------------ threadpool done ------------------");
        Thread.currentThread().join();

    }

    @Test
    public void threadPoolExecutor() throws InterruptedException {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 5, 10L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(35));
        IntStream.range(1, 40).forEach(i -> {
//            try {
//                Thread.sleep(100);
                poolExecutor.execute(() -> {
                    try {
                        Thread.sleep(2000);
//                        int i1 = 1 / 0;
                    } catch (InterruptedException e) {
                        System.out.println("error");
//                        e.printStackTrace();
                    }
                    System.out.println(" ------------ print Thread : " + Thread.currentThread().getName() + " >>> print " + i);
                });
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//                System.out.println("error2");
//            }
        });

        Thread.sleep(2000);
//        poolExecutor.shutdown();
        Thread.currentThread().join();

    }


}
